{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 选择排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 概述\n",
    "- 大致流程:每次从数组中选择最小的数，原数组删去这个元素得到新的数组，重复操作，直到最后只剩下一个元素，即为最大的元素。（从小到大排序的时候）\n",
    "- 算法的时间复杂度$O(n^{2})$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 代码实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-28T12:20:50.361338Z",
     "start_time": "2018-12-28T12:20:50.346287Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 6, 10]\n"
     ]
    }
   ],
   "source": [
    "def findSmallest(arr):\n",
    "    smallest = arr[0]\n",
    "    smallest_index = 0\n",
    "    for i in range(1, len(arr)):\n",
    "        if arr[i] < smallest:\n",
    "            smallest = arr[i]\n",
    "            smallest_index = i\n",
    "    return smallest_index\n",
    "\n",
    "def selectionSort(arr):\n",
    "    newArr = []\n",
    "    for i in range(len(arr)):\n",
    "        smallest = findSmallest(arr)\n",
    "        newArr.append(arr.pop(smallest))\n",
    "    return newArr\n",
    "\n",
    "# test\n",
    "print(selectionSort([5, 3, 6, 2, 10]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 代码实现（更加Pythonic）\n",
    "\n",
    "参考[RC](https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort#Python)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-28T12:41:02.666452Z",
     "start_time": "2018-12-28T12:41:02.659076Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 6, 10]\n"
     ]
    }
   ],
   "source": [
    "def selsort(lst):\n",
    "    for i, e in enumerate(lst):\n",
    "        # 每次迭代之后，i前面的元素均已经按照x(1),...X(i-1)的顺序排好了\n",
    "        # 所以表现为从“依次排序”\n",
    "        mn = min(range(i, len(lst)), key=lst.__getitem__)\n",
    "        lst[i], lst[mn] = lst[mn], lst[i]\n",
    "    return lst\n",
    "\n",
    "# test\n",
    "print(selsort([5, 3, 6, 2, 10]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python语法疑问"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">mn = min(range(i, len(lst)), key=lst.__getitem__)\n",
    ">\n",
    ">根据函数lst.\\_\\_getitem\\_\\_(x)的大小，选出使得lst.\\_\\_getitem\\_\\_(x)最小的x(其中x的范围是i到len(lst)-1)，得到最小的x令其为mn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">lst[i], lst[mn] = lst[mn], lst[i]\n",
    ">\n",
    ">从右往左依次运行，在等号右边运行完后就是lst在mn和i处的值，然后翻别将其赋值给对方原来的位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
